package com.orange.off.cloud.cloudoff.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LHRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.orange.off.cloud.cloudoff.R;
import com.orange.off.cloud.cloudoff.activity.IntermediaryActivity;
import com.orange.off.cloud.cloudoff.base.ListBaseAdapter;
import com.orange.off.cloud.cloudoff.base.SuperViewHolder;
import com.orange.off.cloud.cloudoff.model.RealEstate_ListBean;
import com.orange.off.cloud.cloudoff.photoview.ImagePagerActivity;
import com.orange.off.cloud.cloudoff.utils.Options;
import com.orange.off.cloud.cloudoff.utils.ToastUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * 楼盘资源adapter
 *
 * @author smile
 */

public class RealEstateAdapter extends ListBaseAdapter<RealEstate_ListBean> {

    private RealEstateImageHorizontalDataAdapter mDataAdapter = null;

    private LRecyclerViewAdapter mLRecyclerViewAdapter = null;
    private DisplayImageOptions options;

    public RealEstateAdapter(Context context) {
        super(context);
        options = Options.getHeaderOptions();
    }

    @Override
    public int getLayoutId() {
        return R.layout.real_estate_item;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        ViewHolder viewHolder = new ViewHolder();
        final RealEstate_ListBean item = mDataList.get(position);
        LHRecyclerView mRecyclerView = holder.getView(R.id.list);
        mDataAdapter = new RealEstateImageHorizontalDataAdapter(mContext);
        mDataAdapter.setDataList(item.listPics);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mLRecyclerViewAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadMoreEnabled(false);
        viewHolder.lll_IdentifyRules = holder.getView(R.id.lll_IdentifyRules);//认定规则
        viewHolder.lll_commissionReward = holder.getView(R.id.lll_commissionReward);//佣金奖励
        viewHolder.lll_recommend = holder.getView(R.id.lll_recommend);//推荐客户
        viewHolder.img_RealEstate = (ImageView) holder.getView(R.id.img_RealEstate);//项目头像图片

        viewHolder.tv_RealEstateName = holder.getView(R.id.tv_RealEstateName);//房源项目名称
        viewHolder.tv_CooperationStatus = holder.getView(R.id.tv_CooperationStatus);//合作状态
        viewHolder.tv_TotalMoney = holder.getView(R.id.tv_TotalMoney);//最高佣金
        viewHolder.tv_AveragePrice = holder.getView(R.id.tv_AveragePrice);//均价

        viewHolder.tv_LatestNews = holder.getView(R.id.tv_LatestNews);//最新动态
        viewHolder.tv_DiscountInformation = holder.getView(R.id.tv_DiscountInformation);//优惠信息
        viewHolder.tv_MainPushType = holder.getView(R.id.tv_MainPushType);//主推户型
        viewHolder.tv_ProjectAddress = holder.getView(R.id.tv_ProjectAddress);//项目地址
        viewHolder.tv_phone = holder.getView(R.id.tv_phone);//联系电话
        //        TextView titleText = holder.getView(R.id.info_text);
        //        titleText.setText(item.title);
        ImageLoader.getInstance().displayImage(item.logo, viewHolder.img_RealEstate, options);
        final String renzheng = item.renzheng;
        if ("1".equals(renzheng)) {//1已认证2未认证
            viewHolder.tv_CooperationStatus.setText("已合作");
        } else {
            viewHolder.tv_CooperationStatus.setText("未合作");
        }
        viewHolder.tv_RealEstateName.setText(item.name);
        //        viewHolder.tv_TotalMoney.setText(item.commission);
        viewHolder.tv_AveragePrice.setText("均价" + item.junjia + "/m²");
        viewHolder.tv_LatestNews.setText(item.dongtai);
        viewHolder.tv_DiscountInformation.setText(item.youhui);
        viewHolder.tv_MainPushType.setText(item.huxing);
        viewHolder.tv_ProjectAddress.setText(item.address);
        viewHolder.tv_phone.setText(item.phone);
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (mDataAdapter.getDataList().size() > position) {
                    List<String> lists = new ArrayList<String>();
                    for (int i = 0; i < item.listPics.size(); i++) {
                        lists.add(item.listPics.get(i).image);
                    }
                    Intent intent = new Intent(mContext, ImagePagerActivity.class);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, (Serializable) lists);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    mContext.startActivity(intent);
                }

            }

        });
        //推荐客户：
        viewHolder.lll_recommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(renzheng)) {//1已认证2未认证
                    Intent intent = new Intent(mContext, IntermediaryActivity.class);
                    intent.putExtra("type", IntermediaryActivity.CLIENT_ADD);
                    Bundle bundle = new Bundle();
                    bundle.putString("project_name", item.name);//楼盘名字
                    bundle.putString("name", "");//客户姓名
                    bundle.putString("id", item.id);//楼盘id
                    bundle.putString("sex", "");//客户性别
                    bundle.putString("phone", "");//客户电话
                    bundle.putString("oplist", item.oplist);
                    intent.putExtra("bundle", bundle);
                    mContext.startActivity(intent);
                } else {
                    //跳转到身份认证界面：
                    Intent intent = new Intent(mContext, IntermediaryActivity.class);
                    intent.putExtra("type", IntermediaryActivity.COMMAUTHENTICATION);
                    intent.putExtra("project_name", item.name);
                    intent.putExtra("id", item.id);
                    intent.putExtra("Identity", "1");//指定跳转身份类别1：有项目id及名称。2：个人中心身份认证没有id及项目名称
                    mContext.startActivity(intent);
                }

            }
        });
        //佣金奖励
        viewHolder.lll_commissionReward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(renzheng)) {//1已认证2未认证
                    Intent intent = new Intent(mContext, IntermediaryActivity.class);
                    intent.putExtra("type", IntermediaryActivity.REAL_ESTATE_COMMISSION_FRAGMENT);
                    intent.putExtra("id", item.id);
                    mContext.startActivity(intent);
                } else {
                    ToastUtils.show(mContext, "请先认证项目");
                }

            }
        });
        //报备规则
        viewHolder.lll_IdentifyRules.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(renzheng)) {//1已认证2未认证
                    Intent intent = new Intent(mContext, IntermediaryActivity.class);
                    intent.putExtra("type", IntermediaryActivity.REAL_ESTATE_CERTIFICATION_RULES_FRAGMENT);
                    intent.putExtra("id", item.id);
                    mContext.startActivity(intent);
                } else {
                    ToastUtils.show(mContext, "请先认证项目");
                }

            }
        });
    }

    class ViewHolder {
        LinearLayout lll_IdentifyRules, lll_commissionReward, lll_recommend;
        TextView tv_RealEstateName, tv_CooperationStatus, tv_TotalMoney, tv_AveragePrice,
                tv_LatestNews, tv_DiscountInformation, tv_MainPushType, tv_ProjectAddress, tv_phone;
        ImageView img_RealEstate;
    }
}
